package pre.lxm.demo.chart;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import pre.lxm.demo.R;
import pre.lxm.demo.chart.bean.RecordBean;
import pre.lxm.demo.chart.recycler.ChartDecoration;
import pre.lxm.demo.chart.recycler.RecordChartAdapter;


public class RecordChartActivity extends AppCompatActivity {


    @BindView(R.id.recycler_record)
    RecyclerView recordRecycler;
    @BindView(R.id.tv_result)
    TextView tvResult;
    @BindView(R.id.chartview_1)
    RecordChartView chartview1;
    @BindView(R.id.chartview_2)
    RecordChartView chartview2;
    @BindView(R.id.chartview_3)
    RecordChartView chartview3;
    @BindView(R.id.horizontal_chartview)
    HorizontalChartView horizontalChartview;


    private List<RecordBean> records;

    private String[] results = new String[]{"3", "1", "0"};
    private String[] hosts = new String[]{"h", "g"};
    private RecordChartAdapter mAdapter;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_record_chart);
        ButterKnife.bind(this);
//        initData();
//        initView();

    }

    private void initHorizontalChartView() {
        initData(16);

    }

    private void initChartView() {
        initData(8);
        chartview1.refreshData(records);

        initData(10);
        chartview2.refreshData(records);
        initData(16);
        chartview3.refreshData(records);
    }


    private void initView() {
        LinearLayoutManager llManager = new LinearLayoutManager(this);
        llManager.setOrientation(LinearLayoutManager.HORIZONTAL);
        recordRecycler.setLayoutManager(llManager);
        recordRecycler.addItemDecoration(new ChartDecoration(this));
        mAdapter = new RecordChartAdapter(this, records);
        recordRecycler.setAdapter(mAdapter);
    }

    private void initData(int size) {
        records = new ArrayList<>();
        for (int i = 1; i <= size; i++) {
            RecordBean recordBean = new RecordBean();
            recordBean.setTurn(i + "");
            recordBean.setResult(results[new Random().nextInt(3)]);
            recordBean.setIsHost(hosts[new Random().nextInt(2)]);
            records.add(recordBean);
        }
        Log.e("---", "----" + records.toString());
        StringBuilder sBuilder = new StringBuilder();
        for (RecordBean recordBean : records) {
            sBuilder.append("轮次:" + recordBean.getTurn());
            sBuilder.append(",\t战绩:");
            switch (recordBean.getResult()) {
                case "3":
                    sBuilder.append("胜");
                    break;
                case "1":
                    sBuilder.append("平");
                    break;
                case "0":
                    sBuilder.append("负");
                    break;
            }
            sBuilder.append(",\t主客:");
            switch (recordBean.getIsHost()) {
                case "h":
                    sBuilder.append("主");
                    break;
                case "g":
                    sBuilder.append("客");
                    break;
            }
            sBuilder.append("\t\n");
        }
        tvResult.setText(sBuilder.toString());
    }

    @OnClick(R.id.btn_reload)
    public void btnReloadClick(View view) {
//        initChartView();

//        records.clear();
//        initData();
//        mAdapter.refresh(records);
//
//        initHorizontalChartView();
//        horizontalChartview.refreshData(records);
    }

}
